package com.it.arktech.web;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.it.arktech.model.Member;
import com.it.arktech.repository.jdbc.JdbcMemberRepositoryImpl;


@Controller
public class LoginController {
	
	@Autowired
	JdbcMemberRepositoryImpl repo;
	
	@RequestMapping(value ="loginCheck")
	public String loginCheck(@ModelAttribute Member member, HttpSession session, Model model){
		Member findMember = null;
		try{
			System.out.println("멤버 정보 : "+member.toString());
			findMember = repo.findMemberById(member.getId());
			System.out.println("레파지토리에서 찾은 정보"+findMember.toString());
			if(!member.getPassword().equals(findMember.getPassword()))return "redirect:404";
			session.setAttribute("smember", findMember);
			model.addAttribute("smember", findMember);
			
		}catch(Exception e){
			e.printStackTrace();
			return "404";
		}
		return "index";
	}
	
	
	@RequestMapping(value ="/logout")
	public String logout(HttpSession session){
		try{
			session.invalidate();
			
		}catch(Exception e){
			e.printStackTrace();
			return "404";
		}
		return "index";
	}
	
	
	
	
	
}
